package com.gj.planclass.service.Impl;

import com.gj.planclass.mapper.PlanClassMapper;
import com.gj.planclass.model.*;
import com.gj.planclass.service.PlanClassService;
import com.gj.subclass.enumpackge.StatusEnum2;
import com.gj.utli.AllRecords;
import com.gj.utli.ResultBuilderVo;
import com.gj.utli.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-27.
 */
@Service
public class PlanClassServceImpl implements PlanClassService{
    @Autowired
    private PlanClassMapper planClassMapper;
    @Override
    public ResultVo queryPage(PlanClassServiceVo planClassServiceVo) {
        //查询数据
        List<PlanClassVo> planClassVoList=planClassMapper.queryData(planClassServiceVo);
        for (PlanClassVo planClassVo:planClassVoList){
/*          if(factoryClassVo.getEnabled()==true){
              factoryClassVo.setEnabledName("启用");
            }else if (factoryClassVo.getEnabled()==false){
              factoryClassVo.setEnabledName("禁用");
            }*/
            Boolean status = planClassVo.getStatus();
            Integer status2=status?1:0;
            planClassVo.setStatusName(StatusEnum2.getStatusName(status2));
        }
        //查询数量
        int count=planClassMapper.queryCount(planClassServiceVo);
        AllRecords allRecords=new AllRecords();
        allRecords.setPageIndex(planClassServiceVo.getPageIndex());
        allRecords.setPageSize(planClassServiceVo.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(planClassVoList);
        allRecords.setTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional  //事务
    public ResultVo add( PlanClassAddVo planClassAddVo) {
        //验证编码 名称不能重复
        int count =planClassMapper.queryByName(planClassAddVo.getName());
      /*if (count==1){
          return ResultBuilderVo.error("名称已经存在");
      }
      PlanClassVo planClassVo1 =planClassMapper.queryCode(planClassAddVo.getCode());
        if (planClassVo1==null){
            return ResultBuilderVo.error("编码已经存在");
        }*/
        PlanClassVo planClassVo=new PlanClassVo();
        BeanUtils.copyProperties(planClassAddVo,planClassVo);
        //把左边的值赋给右边  参数值和类型一模一样才可以
        Date nowDate =new Date();
        planClassVo.setCreateTime(nowDate);
        planClassVo.setCreateName("大桥");
        planClassVo.setUpdateTime(nowDate);
        planClassVo.setUpdateName("小乔");
        planClassMapper.insert(planClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid PlanClassUpdateVo planClassUpdateVo) {
        //验证之前的状态  禁用状态下才能修改
        PlanClassVo planClass= planClassMapper.queryById(planClassUpdateVo.getId());
        if (planClass==null){
            return  ResultBuilderVo.error("修改对象不存在");
        }
        if (planClass.getStatus()){
            return ResultBuilderVo.error("禁用状态下才能修改");
        }
        int count=planClassMapper.queryByName(planClassUpdateVo.getName());
        if (count!=0){
            return ResultBuilderVo.error("名称已经存在了");
        }
        PlanClassVo planClassVo=new PlanClassVo();
        BeanUtils.copyProperties(planClassUpdateVo,planClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        planClassVo.setUpdateTime(nowDate);
        planClassVo.setUpdateName("项羽");
        planClassMapper.update(planClassVo);
        return ResultBuilderVo.success();

    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid PlanClassUpdateStatusVo planClassUpdateStatusVo) {
        PlanClassVo planClass= planClassMapper.queryById(planClassUpdateStatusVo.getId());
        if (planClass==null){
            return  ResultBuilderVo.error("修改对象不存在");
        }
        PlanClassVo planClassVo=new PlanClassVo();
        BeanUtils.copyProperties(planClassUpdateStatusVo,planClassVo);//把左边参数赋值给右边
        Date nowDate=new Date();
        planClassVo.setUpdateTime(nowDate);
        planClassVo.setUpdateName("项羽");
        planClassMapper.updateStatus(planClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo seet(Integer id) {
        PlanClassVo planClassVo=planClassMapper.queryById(id);
        if (planClassVo==null){
            return ResultBuilderVo.error("修改对象不存在");
        }
        Boolean status = planClassVo.getStatus();
        Integer status2=status==true?1:0;
        planClassVo.setStatusName(StatusEnum2.getStatusName(status2));
        return ResultBuilderVo.success(planClassVo);
    }
    }

